//
//  TopUpViewController.m
//  marrymarry
//
//  Created by lsj on 16/10/11.
//  Copyright © 2016年 cf. All rights reserved.
//

static NSString * TopUpCellID = @"TopUpTableViewCell";
static NSString * AddVipCellID = @"AddVipTableViewCell";
static NSString * PowerCellID = @"PowerInfoTableViewCell";
#import "TopUpViewController.h"
#import "TopUpTableViewCell.h"
#import "AddVipTableViewCell.h"
#import "PowerInfoTableViewCell.h"

@interface TopUpViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSString * _myYueBiStr;
}
@property (weak, nonatomic) IBOutlet UILabel *MyTitleLabel;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UIView *chosePayView;
@property (weak, nonatomic) IBOutlet UIView *grayView;
@property (weak, nonatomic) IBOutlet UIView *TiShiView;
@property (weak, nonatomic) IBOutlet UIButton *giveUpBtn;
@property (weak, nonatomic) IBOutlet UIButton *goPayBtn;
@property (weak, nonatomic) IBOutlet UIView *myTopUpView;
@property (strong,nonatomic) NSDictionary * myBigDic;
@end

@implementation TopUpViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.hidden = YES;
 [self requestTheNetWith:nil andURL:TOPUP_URL andDic:@{@"userId":[Tools getValueFromNSUserDefaultsByKey:USERID]} andRequestSingStr:@"topup"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.MyTitleLabel.text = @"充值服务";
    self.myBigDic = [NSDictionary new];
    self.myBigDic = [Tools getValueFromNSUserDefaultsByKey:MY_BIGGEST_INFO_DIC];
     _myYueBiStr =  [NSString stringWithFormat:@"%@",self.myBigDic[@"yuebi"]];
//    self.grayView.layer.masksToBounds = YES;
    self.TiShiView.layer.cornerRadius = 8;
    self.giveUpBtn.layer.cornerRadius = 5;
    self.goPayBtn.layer.cornerRadius = 5;
    UITapGestureRecognizer * grayCancelTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goCancel:)];
    [self.grayView addGestureRecognizer:grayCancelTap];

   
}

-(void)goCancel:(UITapGestureRecognizer*)tap
{
    self.TiShiView.hidden = NO;
}
#pragma mark --------- 放弃支付
- (IBAction)sureToGiveUpPay:(UIButton *)sender {
    if (self.chosePayView.hidden == NO) {
       
        self.chosePayView.hidden = YES;
    }else if (self.myTopUpView.hidden == NO)
    {
        self.myTopUpView.hidden = YES;
    }
    self.TiShiView.hidden = YES;
    self.grayView.hidden = YES;
    
}
#pragma mark --------- 立即支付
- (IBAction)sureToPay:(UIButton *)sender {
    self.TiShiView.hidden=YES;
    
}


#pragma mark UITableViewDatasource,UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if (section == 1)
    {return 1;}
    else if (section ==2 )
    {return 3;
    }else{
        return  0 ;
    }
    
    
    
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        TopUpTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:TopUpCellID];
        if (!cell) {
            cell = [[TopUpTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TopUpCellID];
            
        }
       
        cell.BalanceYBLabel.text = _myYueBiStr;
        if ([cell.BalanceYBLabel.text isEqualToString:@""]||[cell.BalanceYBLabel.text isEqualToString:@"<null>"]) {
            cell.BalanceYBLabel.text = @"0";
        }
        cell.TopUpBtn.layer.cornerRadius = 5;
        [cell.TopUpBtn addTarget:self action:@selector(goTopUp:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
    }else if (indexPath.section == 1){
        AddVipTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:AddVipCellID];
        if (!cell) {
            cell = [[AddVipTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AddVipCellID];
            
        }
        cell.openVipBtn.layer.cornerRadius = 5;
        [cell.openVipBtn addTarget:self action:@selector(goOpenVip:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
    }else{
        
        PowerInfoTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:PowerCellID];
        if (!cell) {
            cell = [[PowerInfoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PowerCellID];
            
        }
        [cell loadViewWithIndexPath:indexPath];
        return cell;

    }
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 160;
    }else if (indexPath.section == 1)
    {
        return 120;
    }
    else{
        return 80;
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            NSLog(@"1");
        }else if (indexPath.row == 1)
        {
            NSLog(@"2");
        }else if (indexPath.row == 2)
        {
            NSLog(@"3");
        }
        
    }
    
}


-(void)goTopUp:(UIButton *)topUpBtn
{
    NSLog(@"点击了充值");
    if (self.myTopUpView.hidden) {
        self.myTopUpView.hidden = NO;
        self.grayView.hidden = NO;
    }else {
        self.myTopUpView.hidden = YES;
        self.grayView.hidden = YES;
    }
    
}


-(void)goOpenVip:(UIButton *)openVipBtn
{
    NSLog(@"点击了开通Vip");
    if (self.chosePayView.hidden) {
        self.chosePayView.hidden = NO;
        self.grayView.hidden = NO;
    }else{
        self.chosePayView.hidden = YES;
        self.grayView.hidden = YES;
    }
}

#pragma mark --------------网络请求
-(void)requestTheNetWith:(NSArray *)myRequestArry andURL:(NSString *)myurl andDic:(NSDictionary *)myDic andRequestSingStr:(NSString *)SingStr;
{
    if (myRequestArry.count>0) {
        [[LJFNetworkingManager shareManager] POST:myurl upLoadImages:myRequestArry parameters:myDic success:^(id returnData, int code, NSString *msg) {
            NSLog(@"%@",msg);
            
        } failure:^(NSError *error) {
            NSLog(@"失败");
            
        } showHUD:self.view];
        
    }else
    {
        [[LJFNetworkingManager shareManager] POST:myurl parameters:myDic success:^(id returnData, int code, NSString *msg) {
            NSLog(@"%@",msg);
            if ([SingStr isEqualToString:@"topup"]) {
                if (code == 1) {
                    NSLog(@"%@",returnData);
                    _myYueBiStr =[NSString stringWithFormat:@"%@",returnData[@"moneny"]];
                    [self.myTableView reloadData];
                }
            }else if ([SingStr isEqualToString:@"heartRequest"]){
                if (code == 1) {
                    
                }
            }
            
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
        } showHUD:self.view];
        
    }
    
    
}



- (IBAction)getBackVCBtn:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
